package com.ukao.cashregister.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.mcxtzhang.commonadapter.rv.CommonAdapter;
import com.mcxtzhang.commonadapter.rv.ViewHolder;
import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.bean.ChooseTag;
import com.ukao.cashregister.eventbus.ChooseTimeEvent;
import com.ukao.cashregister.eventbus.UserInfoEvent;
import com.ukao.cashregister.listener.MyTextWatcher;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.SystemUIUtils;
import com.ukao.cashregister.widget.FlowLayoutManager;
import com.ukao.cashregister.widget.StateButton;
import com.ukao.cashregister.widget.StateImageView;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 选择标签
 * Created by ukaodev on 2017/6/2.
 */

public class ChooseTagDialogFragment extends BaseDialogFragment {

    Unbinder unbinder;
    @BindView(R.id.choose_tag_recycler)
    RecyclerView mTagRecycler;
    @BindView(R.id.choose_all_tag_recycler)
    RecyclerView mAllTagRecycler;
    @BindView(R.id.choose_tag_cancel)
    Button mCancel;
    @BindView(R.id.choose_tag_submit)
    Button mSubmit;
    @BindView(R.id.close_btn)
    StateImageView closeBtn;
    @BindView(R.id.title)
    TextView title;

    private List<ChooseTag.ChooseTaglist> myTags;
    private List<ChooseTag.ChooseTaglist> allTags;
    CommonAdapter<ChooseTag.ChooseTaglist> mAdapter;
    CommonAdapter<ChooseTag.ChooseTaglist> mAllAdapter;
    private List<ChooseTag> myTagCaches = new ArrayList<>();
    private Map<Integer, String> mMap = new HashMap<>();
    private ArrayList<String> addTagdata = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        allTags = (ArrayList<ChooseTag.ChooseTaglist>) getArguments().getSerializable(ARG_PARAM1);
        myTags = (ArrayList<ChooseTag.ChooseTaglist>) getArguments().getSerializable(ARG_PARAM2);
        if (allTags == null) allTags = new ArrayList<>();
        if (myTags == null) myTags = new ArrayList<>();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_fragment_choose_tag, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }


    @Override
    protected void initListener() {
        super.initListener();


    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getDialog()!=null){
            SystemUIUtils.setStickFullScreen(getDialog().getWindow());
        }
        initView();
        initData();
    }

    private void initView() {
        title.setText("标签");
        if (!CheckUtils.isEmpty(myTags)) {
            for (int i = 0; i < allTags.size(); i++) {
                ChooseTag.ChooseTaglist allTagItem = allTags.get(i);
                String name = allTagItem.getName();
                for (int i1 = 0; i1 < myTags.size(); i1++) {
                    String mTagName = myTags.get(i1).getName();
                    if (name.equals(mTagName)) {
                        allTagItem.setCheck(true);
                    }
                }

            }
        }


    }


    private void initData() {
        myTags.add(new ChooseTag.ChooseTaglist("添加标签"));
        mAdapter = new CommonAdapter<ChooseTag.ChooseTaglist>(getActivity(), myTags, R.layout.choose_tag_item) {
            @Override
            public void convert(final ViewHolder viewHolder, final ChooseTag.ChooseTaglist item) {
                RadioButton rbt = ((RadioButton) viewHolder.getView(R.id.choose_tag_item_tab));
                final EditText addTag = viewHolder.getView(R.id.add_tag);
                final StateButton saveBtn = viewHolder.getView(R.id.save_btn);
                rbt.setText(CheckUtils.isEmptyString(item.getName()));
                rbt.setChecked(true);

                if (viewHolder.getLayoutPosition() == getDatas().size() - 1) {
                    rbt.setText("");
                    addTag.setHint(item.getName());
                    saveBtn.setVisibility(View.VISIBLE);
                    addTag.setVisibility(View.VISIBLE);
                    rbt.setVisibility(View.GONE);
                    addTag.addTextChangedListener(new MyTextWatcher() {
                        @Override
                        public void afterTextChanged(Editable s) {
                            super.afterTextChanged(s);
                            mMap.put(getPosition(viewHolder), s.toString().trim());

                        }
                    });

                    saveBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            addTag(viewHolder, addTag);
                        }
                    });
//                    TextView.OnEditorActionListener mOnEditorActionListener = new TextView.OnEditorActionListener() {
//                        @Override
//                        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                            if (actionId == EditorInfo.IME_ACTION_DONE) {
//                                addTag(viewHolder, addTag);
//
//                            }
//                            return false;
//                        }
//                    };
//                    addTag.setOnEditorActionListener(mOnEditorActionListener);


                } else {
                    rbt.setVisibility(View.VISIBLE);
                    rbt.setChecked(true);
                    addTag.setVisibility(View.GONE);
                    saveBtn.setVisibility(View.GONE);
                    rbt.setTextColor(mContext.getResources().getColor(R.color.green));
                }
            }

            private void addTag(ViewHolder viewHolder, EditText addTag) {
                String strTag = mMap.get(getPosition(viewHolder));
                boolean isallTagContain = false;
                boolean isMyTagContain = false;


                if (!CheckUtils.isEmpty(strTag)) {
                    addTag.setText("");
                    for (int i = 0; i < myTags.size(); i++) {
                        ChooseTag.ChooseTaglist myTag = myTags.get(i);
                        if (myTag.getName().equals(strTag)) {
                            isMyTagContain = true;
                            if (i != 0) {
                                myTags.remove(i);
                                myTags.add(0, myTag);

                            }

                        }
                    }

                    for (int i = 0; i < allTags.size(); i++) {
                        ChooseTag.ChooseTaglist item = allTags.get(i);
                        if (item.getName().equals(strTag)) {
                            isallTagContain = true;
                            if (!isMyTagContain) {
                                item.setCheck(true);
                                myTags.add(0, item);
                            }

                        }
                    }

                    if (!isallTagContain && !isMyTagContain) {
                        ChooseTag.ChooseTaglist mtag = new ChooseTag.ChooseTaglist(strTag);
                        mtag.setCheck(true);
                        allTags.add(0, mtag);
                        myTags.add(0, new ChooseTag.ChooseTaglist(strTag));

                    }

                    notifyDataSetChanged();
                    mAllAdapter.notifyDataSetChanged();

                }
            }


        };

        mTagRecycler.setLayoutManager(new FlowLayoutManager());
        mTagRecycler.setAdapter(mAdapter);
        mAllAdapter = new CommonAdapter<ChooseTag.ChooseTaglist>(getActivity(), allTags == null ? new ArrayList<ChooseTag.ChooseTaglist>() : allTags, R.layout.choose_tag_item) {
            @Override
            public void convert(ViewHolder viewHolder, final ChooseTag.ChooseTaglist chooseTag) {
                RadioButton rbt = ((RadioButton) viewHolder.getView(R.id.choose_tag_item_tab));

                rbt.setText(chooseTag.getName());
                rbt.setChecked(chooseTag.isCheck());
                rbt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        chooseTag.setCheck(!chooseTag.isCheck());
                        if (chooseTag.isCheck()) {
                            myTags.add(0, chooseTag);
                        } else {
                            for (ChooseTag.ChooseTaglist myTag : myTags) {
                                if (myTag.getName().equals(chooseTag.getName())) {
                                    myTags.remove(myTag);
                                    break;
                                }
                            }
                        }
                        notifyDataSetChanged();
                        mAdapter.notifyDataSetChanged();
                    }
                });
            }
        };
        mAllTagRecycler.setLayoutManager(new FlowLayoutManager());
        mAllTagRecycler.setAdapter(mAllAdapter);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Subscribe
    public void onEvent(ChooseTimeEvent event) {

    }

    @OnClick({R.id.choose_tag_cancel, R.id.choose_tag_submit,R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case  R.id.close_btn:

                dismiss();
                break;

            case R.id.choose_tag_cancel:
                dismiss();
                break;
            case R.id.choose_tag_submit:
                UserInfoEvent.postHasData(UserInfoEvent.Message.TAG, myTags);
                dismiss();
                break;
        }
    }
}
